﻿using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.ServiceContracts;
using KendoGridBinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkTicket.Application.Implementation;
using WorkTicket.Website.Common;

namespace WorkTicket.Website.Controllers
{
    public class ColorGroupController : Controller
    {
        protected IColorGroupService colorGroupService = new ColorGroupServiceImpl();
        protected IColorService colorService = new ColorServiceImpl();
        //
        // GET: /ColorGroup/

        public ActionResult Index()
        {
            return View();
        }


        public JsonResult GetAllItems(KendoGridRequest request)
        {
            List<ColorGroup> result = colorGroupService.GetAll();
            return Json(new { data = result, total = result.Count },JsonRequestBehavior.AllowGet); 
        }

        //
        // GET: /ColorGroup/Create
        [HttpGet]
        public ActionResult Create()
        {
            ColorGroup colorGroup = new ColorGroup();
            colorGroup.Colors = new List<Color>();
            ViewData["SelectedColorList"] = from a in colorGroup.Colors
                                            select new SelectListItem
                                            {
                                                Text = a.Name,
                                                Value = a.ColorID.ToString()
                                            };
            this.GetColorList(colorGroup);
            ViewData["HideValue"] = string.Empty;
            return View(colorGroup);
        }

        //
        // POST: /ColorGroup/Create

        [HttpPost]
        public ActionResult CreateAndEdit(ColorGroup model)
        {
            string strTemp = Request["hdSelectedColor"];
            string[] strList = strTemp.Split(';');

            model.Colors = new List<Color>();
            model.ColorGroupColor = new ColorGroupColor();
            foreach (var item in strList)
            {
                if (item != "")
                {
                    Color color = new Color();
                    color.ColorID = Guid.Parse(item.Split(',')[0]);
                    color.Name = item.Split(',')[1];
                    model.Colors.Add(color);
                }
            }
            
            try
            {
                // TODO: Add insert logic here
                if (Request.IsAjaxRequest())
                {
                    if (model.ColorGroupID.ToString() == "00000000-0000-0000-0000-000000000000") //add new entity
                    {
                        model.ColorGroupID = Guid.NewGuid();
                        if (ModelState.IsValid)
                        {
                            colorGroupService.Add(model);
                        }
                        else
                        {
                            return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
                        }
                    }
                    else  //update entity
                    {
                        if (ModelState.IsValid)
                        {
                            colorGroupService.Update(model);
                        }
                    }
                    
                }
                return Json(new { data = true, message = "OK" }, JsonRequestBehavior.AllowGet); 
            }
            catch
            {
                return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
            }
        }

        //
        // GET: /ColorGroup/Edit/5
        [HttpGet]
        public ActionResult Edit(string id)
        {
            var colorGroup= colorGroupService.GetItemByID(id);
            ViewData["SelectedColorList"] = from a in colorGroup.Colors
                                    select new SelectListItem
                                    {
                                        Text = a.Name,
                                        Value = a.ColorID.ToString()
                                    };
            string strTemp = string.Empty;
            foreach (var item in colorGroup.Colors)
            {
                strTemp = strTemp + item.ColorID.ToString() + "," + item.Name + ";";
            }
            ViewData["HideValue"] = strTemp;
            this.GetColorList(colorGroup);
            return View("Create",colorGroup);
        }

        //
        // GET: /ColorGroup/Delete/5o
        public ActionResult Delete(string id)
        {
            try
            {
                colorGroupService.Delete(id);
                return Json(new { message = "ok" },JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { message = "no" }, JsonRequestBehavior.AllowGet);
            }
        }


        [HttpPost]
        public JsonResult CheckNameExisted()
        {
            string name = Request["Name"];
            string id = Request["ID"];
            JsonResult result = new JsonResult();
            result.Data = colorGroupService.CheckNameExisted(name, Guid.Parse(id));
            return result;
        }

        private void GetColorList(ColorGroup colorGroup)
        {
            List<SelectListItem> sourceList = new List<SelectListItem>();
            foreach (var item in colorService.GetAll())
            {
                if (!colorGroup.Colors.Exists(c => c.ColorID == item.ColorID))
                {
                    sourceList.Add(new SelectListItem { Text = item.Name, Value = item.ColorID.ToString() });
                }
            }
            ViewData["ColorList"] = sourceList.AsEnumerable();

        }


    }
}
